<template>
  <div class="regist-wrapper">
    <div class="regist-container" v-if="step !== 3">
      <div class="regist-header">学生账号激活</div>
      <div class="regist-form" v-if="step == 1">
        <a-form
          ref="formRef"
          :model="formState"
          :label-col="{ span: 5 }"
          :rules="rules"
          :wrapper-col="{ span: 119 }"
          autocomplete="off"
          @finish="handleStep1"
        >
          <div class="form-content">
            <a-form-item class="form-item" label="姓名" name="real_name">
              <a-input v-model:value="formState.real_name" />
            </a-form-item>

            <a-form-item class="form-item" label="账号/手机号" name="user_name">
              <a-input v-model:value="formState.user_name" />
            </a-form-item>
            <a-form-item class="form-item" label="设定密码" name="password">
              <a-input-password v-model:value="formState.password" />
            </a-form-item>
            <a-form-item class="form-item" label="验证码" name="captcha">
              <div class="flex">
                <a-input v-model:value="formState.captcha" />
                <img :src="codeImg" class="code" @click="handleChageCode" />
              </div>
            </a-form-item>
          </div>
          <div class="regist-button">
            <a-button class="btn" type="primary" html-type="submit"
              >下一步</a-button
            >
          </div>
        </a-form>
      </div>
      <div class="regist-form" v-if="step == 2">
        <a-form
          ref="formRef2"
          :model="formState2"
          :label-col="{ span: 5 }"
          :wrapper-col="{ span: 119 }"
          :rules="rules2"
          @finish="handleStep2"
        >
          <div class="form-content">
            <a-form-item class="form-item" label="教师" name="teacher_name">
              <a-input
                v-model:value="formState2.teacher_name"
                placeholder="请输入教师姓名"
              />
            </a-form-item>
            <a-form-item class="form-item" label="邀请码" name="invide_code">
              <a-input
                v-model:value="formState2.invide_code"
                placeholder="请输入班级邀请码"
              />
            </a-form-item>
          </div>
          <div class="regist-button">
            <a-button class="btn" type="primary" html-type="submit"
              >下一步</a-button
            >
          </div>
        </a-form>
      </div>
    </div>
    <div class="success-wrapper" v-if="step == 3">
      <div class="success-main">
        <img src="@/assets/images/regist-success.png" class="success-img" />
        <div class="success-content">
          <div class="success-title">
            欢迎你，{{ datas?.student?.user?.real_name }}同学
          </div>
          <div class="success-desc">
            你已成功加入{{ datas?.clazz?.cusomer_name
            }}{{
              datas?.clazz?.teacher_name
            }}老师的班级，如需加入其他老师的班级，请在“我的-我的班级”模块中继续添加。
          </div>
        </div>
      </div>
      <div class="success-button" @click="handleToHome">进入平台</div>
    </div>
    <div class="regist-footer">
      <p>Copyright © 2012-2023</p>
      <p>天津良益科技股份有限公司版权所有 | 蜀ICP备13028371</p>
    </div>
  </div>
</template>
<script setup>
import api from "@/api/user";
import { setAuth } from "@/utils/auth";
import { computed, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const formState = ref({
  real_name: "",
  user_name: "",
  password: "",
  captcha: "",
});
const step = ref(1);

const validate1 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请输入姓名");
  } else {
    return Promise.resolve();
  }
};
const validate2 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请输入登录名");
  } else {
    return Promise.resolve();
  }
};
const validate3 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请设定密码");
  } else {
    return Promise.resolve();
  }
};
const validate4 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请输入验证码");
  } else {
    return Promise.resolve();
  }
};
const validate5 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请输入教师姓名");
  } else {
    return Promise.resolve();
  }
};
const validate6 = async (_rule, value) => {
  if (value === "") {
    return Promise.reject("请输入班级邀请码");
  } else {
    return Promise.resolve();
  }
};
const rules = {
  user_name: [{ validator: validate2, trigger: "change" }],
  real_name: [{ validator: validate1, trigger: "change" }],
  password: [{ validator: validate3, validate: "change" }],
  captcha: [{ validator: validate4, validate: "change" }],
};

const formState2 = ref({
  teacher_name: "",
  invide_code: "",
});
const rules2 = ref({
  teacher_name: [{ validator: validate5, trigger: "change" }],
  invide_code: [{ validator: validate6, trigger: "change" }],
});
const timestamp = ref(new Date().getTime());
const codeImg = computed(
  () =>
    import.meta.env.VITE_API_URL +
    "/api/User/GifHybridCode" +
    "?timestamp=" +
    timestamp.value
);
const formRef = ref();
const handleStep1 = () => {
  formRef.value.validate().then(async () => {
    try {
      const res = await api.UserRegister({
        ...formState.value,
        timestamp: timestamp.value,
      });
      console.log(res);
      if (res.accessToken) {
        localStorage.setItem("token", res.accessToken);
        let info = await api.MyProfile();
        info.user_type_id = 4;
        localStorage.setItem("userInfo", JSON.stringify(info));
        await setAuth(info.user_type_id);
        step.value = 2;
      }
    } catch (err) {
      handleChageCode();
    }
  });
};
const formRef2 = ref();
const datas = ref();
const handleStep2 = () => {
  formRef2.value.validate().then(async () => {
    const res = await api.StudentJoinClass(formState2.value);
    const dataRes = await api.ClassStudentFetchModelWithDetails({
      class_student_id: res.class_student_id,
    });
    datas.value = dataRes;
    step.value = 3;
  });
};
const handleChageCode = () => {
  timestamp.value = new Date().getTime();
};
const handleToHome = () => {
  router.push("/home");
};
</script>
<style lang="less" scoped>
.regist-wrapper {
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, #e9f2ff 0%, #eef6ff 100%);
  display: flex;
  box-sizing: border-box;
  padding: 30px 0;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  .regist-container {
    flex: 1;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    .regist-header {
      height: 33px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 24px;
      color: #111a34;
      line-height: 33px;
      text-align: center;
      font-style: normal;
      margin-bottom: 40px;
    }
    .regist-form {
      .form-content {
        height: 360px;
      }
      .form-item {
        width: 508px;
        display: flex;
        /deep/.ant-input {
          height: 60px;
          background: #ffffff;
          border-radius: 12px;
        }
        /deep/ .ant-form-item-row {
          width: 100%;
          display: flex;
          align-items: center;
        }
        /deep/ .ant-input-affix-wrapper .ant-input {
          height: 51px;
        }
        /deep/ .ant-form-item-label label {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 18px;
          color: #262626;
          line-height: 25px;
          font-style: normal;
          margin-right: 16px;
          &::after {
            display: none;
          }
        }
        /deep/.ant-form-item-control-input-content {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        .flex {
          display: flex;
          align-items: center;
          justify-content: space-between;
          width: 100%;
          .code {
            width: 122px;
            height: 60px;
            background: #f7f7f7;
            border-radius: 12px;
            margin-left: 12px;
          }
        }
      }
    }
    .regist-button {
      width: 508px;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      .btn {
        width: 400px;
        height: 60px;
      }
    }
  }
  .regist-footer {
    p {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 12px;
      color: #9ca6b1;
      line-height: 17px;
      text-align: center;
      font-style: normal;
      margin: 0;
    }
  }
}
.success-wrapper {
  display: flex;
  align-items: center;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  .success-main {
    width: 520px;
    justify-content: space-between;
    display: flex;
    align-items: self-start;
    margin-bottom: 20%;
  }
  .success-img {
    width: 224px;
  }
  .success-content {
    .success-title {
      height: 33px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 24px;
      color: #111a34;
      line-height: 33px;
      text-align: left;
      font-style: normal;
      margin-bottom: 28px;
    }
    .success-desc {
      width: 200px;
      height: 80px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #595959;
      line-height: 20px;
      text-align: left;
      font-style: normal;
    }
  }
  .success-button {
    width: 400px;
    height: 60px;
    background: #2776ff;
    border-radius: 12px;
    cursor: pointer;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 18px;
    color: #ffffff;
    line-height: 25px;
    font-style: normal;
    text-align: center;
    line-height: 60px;
  }
}
</style>
